import 'dart:async';
import 'dart:io'; //导IO包
import 'dart:convert'; //解码和编码JSON
import 'dart:convert' as convert;
import 'Log.dart';
import 'ProxyResults.dart';
void main() {
  print(convert.jsonEncode(new ProxyResults(200,msg: "666")));
  proxyRequst();

}

_get() async {
  var responseBody;
  //1.创建HttpClient
  var httpClient = new HttpClient();
  var url = 'http://ee.voidcn.com/article/p-efsuubrn-byx.html';
// url='http://www.baidu.com';
  //2.构造Uri
  try {
    var requset =  await httpClient.getUrl(Uri.parse(url))
//        .catchError((() {
//      print("999");
//    }),test: (f){
//
//    })
        .then((HttpClientRequest proxyRequest) {
          var value='';
      proxyRequest.write(value);
    });

    //3.关闭请求，等待响应
    var response =   requset.close();

    //4.进行解码，获取数据
    if (response.statusCode == 200) {
      //拿到请求的数据
      responseBody = await response.transform(utf8.decoder).join();
      //先不解析打印数据
      print(responseBody);
    } else {
      print("error");
    }
  } on Exception catch (e, s) {
    print('异常'+e.toString() );
  }
  print('object');
}



/**
 * 执行请求
 */
void proxyRequst( )async {

  var url = 'http://download.lizhanqi.top:8899/article/p-efsuubrn-byx.html';
  var  tmpReqTag='2222222222';
  try {
    var proxyHttpClient   = new HttpClient();
    var requset =  await    proxyHttpClient. openUrl('GET', Uri.parse(url))

    .then((HttpClientRequest proxyRequest) {
      return proxyRequest.close();
    }  )
      .then((HttpClientResponse proxyResponse) {
      proxyResponse.transform(convert.utf8.decoder).join().then((    String value) {  });
    }  )    .timeout(const Duration(seconds:5))
//   .catchError((){},test: (a){
//      print(a);
//      return a;
//    })
    ;
  }  on TimeoutException catch (e) {
    print('超时');
  }
  on SocketException catch (e) {
    print('连接失败');
  }
  on Exception catch (e, s) {
    print(e);
  }

}
